//
//  ViewController.m
//  HumorousScripts
//
//  Created by 李欢 on 15/6/15.
//  Copyright (c) 2015年 李欢. All rights reserved.
//

#import "ViewController.h"
//#import "RecommendViewController.h"
#import "VideoViewController.h"
#import "PictureViewController.h"
#import "EassayViewController.h"
#import "AppDelegate.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;

//放置按钮
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *rollView;
//存放按钮数组
@property (nonatomic, retain) NSMutableArray *buttonArray;
//创建4个类的子视图
//@property (nonatomic, retain) RecommendViewController * recommend;
@property (nonatomic, retain) VideoViewController * video;
@property (nonatomic, retain) PictureViewController * picture;
@property (nonatomic, retain) EassayViewController * eassay;
@end

@implementation ViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.buttonArray = [NSMutableArray array];

    }
    return self;
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.buttonArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //    设置透明色
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(itemAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:1.000 green:0.420 blue:0.798 alpha:1.000];
    [self.view setBackgroundColor:[UIColor colorWithRed:0.782 green:1.000 blue:0.026 alpha:1.000]];
    [self createView];
    [self createScrollView];
    
    
}

- (void)itemAction:(id)sender{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    [delegate.sideViewController showLeftViewController:NO];
    
}

- (void)createView
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 40/667.0*Height)];
    [_headerView setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:self.headerView];
    
    // 创建数组，存放标题
    NSArray *array = @[@"段子", @"图片", @"视频"];
    for (int i = 0; i < 3; i++) {
        // 循环创建4个button，分别赋值
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3.0 * i, 0, self.view.frame.size.width / 3.0, self.view.frame.size.height / 667 * 38/667.0*Height)];
        [button setTitle:array[i] forState:UIControlStateNormal];
        //        设置button字体大小
        button.titleLabel.font = [UIFont systemFontOfSize:17/375.0*Width];
        button.tag = i + 1;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:button];
        [self.buttonArray addObject:button];
        //        设置默认背景颜色
        if (i == 0) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
//    [_headerView release];
    
}

#pragma mark - button的点击事件
- (void)buttonAction:(id)sender
{
    // 从上面获取button的tag值，计算scrollView的偏移量
    UIButton *button = (UIButton *)sender;
    self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 1), 0);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.rollView.frame = CGRectMake(self.view.frame.size.width / 3.0 * (button.tag - 1), self.view.frame.size.height / 667 * 38/667.0*Height, self.view.frame.size.width / 3.0, self.view.frame.size.height / 667 * 2);
    }];
    // 滑动改变按钮颜色
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}

- (void)createScrollView
{
    // 创建4个子视图
//    self.recommend = [[RecommendViewController alloc]init];
    self.picture = [[PictureViewController alloc]init];
    self.video = [[VideoViewController alloc]init];
    self.eassay = [[EassayViewController alloc]init];
    
//    [self addChildViewController:self.recommend];
    [self addChildViewController:self.picture];
    [self addChildViewController:self.video];
    [self addChildViewController:self.eassay];
    
    // 创建scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 40/667.0*Height, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 667 * 40/667.0*Height)];
    _scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3,0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    
//    [self.scrollView addSubview:self.recommend.view];
    [self.scrollView addSubview:self.picture.view];
    [self.scrollView addSubview:self.video.view];
    [self.scrollView addSubview:self.eassay.view];
    
    // 分别设置子视图的size
//    self.recommend.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.picture.view.frame = CGRectMake(self.view.frame.size.width * 1.0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.video.view.frame = CGRectMake(self.view.frame.size.width * 2.0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.eassay.view.frame = CGRectMake(self.view.frame.size.width * 0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.view addSubview:self.scrollView];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.rollView.frame = CGRectMake(self.scrollView.contentOffset.x / 3, self.view.frame.size.height / 667 *38/667.0*Height, self.view.frame.size.width / 3, self.view.frame.size.height / 667 * 2.0);
    }];
    //    按钮滑动变色
    for (UIButton * button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    UIButton * button = [self.buttonArray objectAtIndex:self.scrollView.contentOffset.x / self.view.frame.size.width];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
